import { onMounted, ref } from "vue";
import { getCategoryAPI } from "@/apis/category";
import { useRoute } from "vue-router";
import { onBeforeRouteUpdate } from "vue-router";
export function useCategory() {
    // 解决一级分类切换时，组件复用导致分类数据无法更新：监听路由变化
    // 路由变化将分类接口重新发送

    onBeforeRouteUpdate((to) => {
        console.log("路由变化了");
        getCategory(to.params.id)
    })

    const categoryData = ref({});
    const route = useRoute();
    const getCategory = async (id = route.params.id) => {
        const res = await getCategoryAPI(id);
        categoryData.value = res.result;
    };
    onMounted(() => {
        getCategory()
    });
    return {
        categoryData
    }
}